﻿#include "preprocess.h"
#include "location.h"
#include "brightness.h"
#include "postprocess.h"

int main()
{
    try {
        // param in
        std::map<std::string, std::map<std::string, std::string>> ini_data;
        auto param = read_ini("./pre_param.ini");
        std::cout << "param readin" << std::endl;

        std::string img_path = param["Database"]["img_path"];
        std::string single_color = param["Database"]["img_color"]; //单图颜色
        std::string color = "N"; //多图中单图颜色
        int panel_res_rows = std::stoi(param["Panel"]["panel_res_rows"]);
        int panel_res_cols = std::stoi(param["Panel"]["panel_res_cols"]);
        int position = std::stoi(param["Database"]["position"]);

        std::string files_path = param["Database"]["data_path"];
        std::string img_type = param["Database"]["pattern_type"];
        //std::string location_files = param["Database"]["location_pattern"];
        //std::string gray_files = param["Database"]["gray_pattern"];
        std::string color_list = param["Database"]["location_color"];

        std::string camera_index = param["Database"]["camera_index"];
        std::vector<std::string> files_index, color_list_sub, single_img_name;
        string_split(camera_index, files_index);
        string_split(color_list, color_list_sub);
        string_split(img_path, single_img_name);

        // inital
        int location_idx = 0;
        bool is_location_map = true;
        cv::Mat mapx, mapy;
        double mapping = 0;
        std::string name;
        for (int img_idx = 0; img_idx < size(files_index); img_idx++) {
            // image readin
            std::ifstream f(img_path);
            cv::Mat img;
            if (f.good()) { // 单张提取
                img = readImage(img_path);
                is_location_map = true;
                name = single_img_name[size(single_img_name)];
            }
            else {
                img = readImage(files_path + "/" + files_index[img_idx] + "." + img_type); // 多图提取
                if (img_idx == 0 || files_index[img_idx - 1][0] != files_index[img_idx][0]) {
                    is_location_map = true;
                    color = color_list_sub[location_idx];
                    location_idx++;
                    single_color = color;
                }
                else {
                    is_location_map = false;
                    single_color = files_index[img_idx][0];
                }
                name = files_index[img_idx];
            }

            std::cout << name+":img readin" << std::endl;

            // image preprocess
            img_flip(img, position);

            // image location
            if (is_location_map == true) {
                mapx.create(panel_res_cols * 2, panel_res_rows * 2, CV_32FC1); // 释放前部内存并创建新内存空间与指针
                mapy.create(panel_res_cols * 2, panel_res_rows * 2, CV_32FC1);
                std::cout << name+":location start" << std::endl;
                get_map(img, mapx, mapy, mapping, panel_res_rows, panel_res_cols, single_color);
                std::cout << name+":location end" << std::endl;
            }

            // image get brt
            cv::Mat csv;
            std::cout << name+":getting brightness" << std::endl;
            csv = get_brt(img, mapx, mapy, mapping, panel_res_rows, panel_res_cols);
            std::cout << name+":getting brightness end" << std::endl;

            // image postprocess
            csv_flip(csv);
            hole_fill(csv);
            std::cout << name+":hole fill" << std::endl;
            corner_fill(csv);
            std::cout << name+":corner fill" << std::endl;

            // image saving
            std::cout << name+":csv saving start" << std::endl;
            if (f.good()) { // 单张提取
                csv_saving(csv, { "./single_img.csv" }, 6);
            }
            else {
                std::string name = "./" + files_index[img_idx] + ".csv";
                csv_saving(csv, name, 6);
            }
            std::cout << name+":csv saving end" << std::endl;
        }
    }
    catch (const std::exception& e) {
        std::cerr << "Error:  " << e.what() << "\n";
    }
}


//Mat image = imread("84.jpg", IMREAD_COLOR);

//cv::Mat img;
//img = image;
//img.convertTo(img, CV_32F);

//// Normalize pixel values
//cv::Mat normalized_image;
//img.convertTo(normalized_image, CV_32F, 1.0 / 255.0);

//// Resize image
//resize(normalized_image, normalized_image, cv::Size(640, 640));

//cv::Scalar mean(0, 0, 0);
//cv::Scalar std(0.5, 0.5, 0.5);

//normalized_image = (normalized_image - mean) / std;
//
////224 for resnet 640 for yolov8
//cv::Mat inputBlob = cv::dnn::blobFromImage(normalized_image, 1.0, cv::Size(), cv::Scalar(), true, false);

//cv::dnn::Net net = cv::dnn::readNetFromONNX("best.onnx");
//net.setInput(inputBlob);

//cv::Mat result = net.forward();
//cv::Point classIdPoint;